<!-- 全单打折弹框（Vue3） -->
<template>
  <CustomDialog v-model:visible="dialogVisible" top="8vh" curent-class="full-order-discount-dialog">
    <div class="full-discount-container">
      <div class="title">全单打折</div>

      <div class="tabs">
        <div
          class="tab-item"
          :class="{ active: tabActiveIndex === index }"
          v-for="(tab, index) in tabList"
          :key="tab.name"
          @click="handleChangeTab(index, tab.number)"
        >
          {{ tab.name }}
        </div>
      </div>

      <div class="input-row">
        <span class="label">折扣值</span>
        <div class="input-container">
          <input
            class="input"
            v-model.trim="dialogKeyword"
            type="text"
            placeholder="请输入折扣值"
          />
          <img
            class="clear-icon"
            @click="handleClearInput"
            v-show="dialogKeyword"
            src="@/assets/images/home/input-clear.png"
            alt="清除"
          />
        </div>
      </div>

      <NumberKeyboard
        v-model:keyword="dialogKeyword"
        :showRadixPoint="true"
        :isFirstZero="false"
        :maxStrLimit="3"
        :loading="submitLoading"
        @onInputKeyboard="onInputKeyboard"
        @onCancel="handleClearInput"
        @onConfirm="handleConfirm"
      />

      <div class="footer">
        <el-button class="revoke-btn" :loading="revokeLoading" @click="handleRevoke"
          >撤销打折</el-button
        >
      </div>
    </div>
  </CustomDialog>
</template>

<script setup lang="ts">
import { ref, computed, watch } from 'vue'
import { ElMessage } from 'element-plus'
import CustomDialog from '@/components/CustomDialog/Index.vue'
import NumberKeyboard from '@/components/NumberKeyboard/Index.vue'
import { allOrderDiscount } from '@/api/food'

interface Props {
  modelValue: boolean
  deskDetail?: any
}

interface Emits {
  (e: 'update:modelValue', value: boolean): void
  (e: 'success'): void
  (e: 'refreshDesk'): void
}

const props = withDefaults(defineProps<Props>(), {
  modelValue: false,
  deskDetail: null
})

const emit = defineEmits<Emits>()

const dialogVisible = computed({
  get: () => props.modelValue,
  set: val => emit('update:modelValue', val)
})

const tabList = ref([
  { name: '八折', number: '8' },
  { name: '九折', number: '9' },
  { name: '七折', number: '7' },
  { name: '五折', number: '5' }
])
const tabActiveIndex = ref(-1)
const dialogKeyword = ref('')
const submitLoading = ref(false)
const revokeLoading = ref(false)

watch(
  () => props.modelValue,
  val => {
    if (val) resetDatas()
  }
)

const resetDatas = () => {
  tabActiveIndex.value = -1
  dialogKeyword.value = ''
}

const handleClearInput = () => {
  dialogKeyword.value = ''
}

const onInputKeyboard = (val: string) => {
  dialogKeyword.value = val
}

const handleChangeTab = (index: number, number: string) => {
  tabActiveIndex.value = index
  dialogKeyword.value = number
}

const handleConfirm = async () => {
  if (!dialogKeyword.value) {
    ElMessage.warning('折扣值不能为空')
    return
  }
  if (!props.deskDetail?.id) {
    ElMessage.warning('当前桌台信息异常')
    return
  }
  submitLoading.value = true
  try {
    const res = await allOrderDiscount({
      record_id: props.deskDetail.id,
      discount: dialogKeyword.value
    })
    if (res.status === 200) {
      ElMessage.success(res.data?.message || '操作成功')
      dialogVisible.value = false
      emit('success')
      emit('refreshDesk')
    } else {
      ElMessage.error(res.data?.message || '操作失败')
    }
  } catch (e) {
    ElMessage.error('操作失败')
  } finally {
    submitLoading.value = false
  }
}

const handleRevoke = async () => {
  if (!props.deskDetail?.id) {
    ElMessage.warning('当前桌台信息异常')
    return
  }
  revokeLoading.value = true
  try {
    const res = await allOrderDiscount({
      record_id: props.deskDetail.id,
      discount: 10
    })
    if (res.status === 200) {
      ElMessage.success(res.data?.message || '撤销成功')
      dialogVisible.value = false
      emit('success')
      emit('refreshDesk')
    } else {
      ElMessage.error(res.data?.message || '撤销失败')
    }
  } catch (e) {
    ElMessage.error('撤销失败')
  } finally {
    revokeLoading.value = false
  }
}
</script>

<style scoped lang="scss">
.full-discount-container {
  //   width: 984px;
  box-sizing: border-box;
}

.title {
  font-size: 20px;
  font-family:
    PingFang SC,
    PingFang SC-Bold;
  font-weight: 700;
  text-align: center;
  color: #333333;
}

.tabs {
  position: relative;
  height: 88px;
  background: #f4f4f4;
  padding: 10px 20px;
  margin-top: 20px;
  display: flex;
  align-items: center;
  .tab-item {
    width: 120px;
    height: 48px;
    line-height: 48px;
    background: #ffffff;
    border: 1px solid #d2d2d2;
    border-radius: 12px;
    text-align: center;
    font-size: 16px;
    color: #333333;
    cursor: pointer;
    margin-right: 11px;
  }
  .active {
    color: #7c0a03;
    border-color: #7c0a03;
  }
}

.input-row {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  margin: 30px 50px;
  .label {
    font-size: 16px;
    color: #333333;
    margin-right: 17px;
  }
  .input-container {
    display: flex;
    align-items: center;
    width: 420px;
    height: 48px;
    padding: 12px 18px 14px;
    background: #ffffff;
    border: 1px solid #d2d2d2;
    border-radius: 12px;
    box-sizing: border-box;
    .input {
      outline: none;
      border: none;
      width: 350px;
      font-size: 14px;
      color: #333333;
    }
    .clear-icon {
      flex-shrink: 0;
      width: 20px;
      height: 20px;
      cursor: pointer;
    }
  }
}

.footer {
  margin-top: 36px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 35px;
  .revoke-btn {
    width: 167px;
    height: 52px;
    border: 1px solid #333333;
    border-radius: 12px;
    font-size: 18px;
    color: #353535;
  }
}
</style>
<style>
.full-order-discount-dialog {
  max-height: unset !important;
  padding: 50px 0px !important;
}
</style>
